package com.wfs.myapp.controller.bk;

import com.wfs.myapp.common.ApiResult;
import com.wfs.myapp.common.Constant;
import com.wfs.myapp.entity.domain.bk.BkFeedback;
import com.wfs.myapp.entity.domain.bk.BkTalkRecord;
import com.wfs.myapp.entity.dto.req.bk.BirthdayRemindReqDto;
import com.wfs.myapp.entity.dto.req.bk.FeedbackReqDto;
import com.wfs.myapp.entity.dto.req.bk.GenerateTalkRecordReqDto;
import com.wfs.myapp.entity.dto.req.bk.SearchCollectionReqDto;
import com.wfs.myapp.entity.vo.*;
import com.wfs.myapp.service.BkSearchService;
import com.wfs.myapp.service.GlobalService;
import com.wfs.myapp.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/bk/global")
@Api(value = "全局", tags = "全局")
@Slf4j
public class GlobalController {

    @Autowired
    private BkSearchService searchService;

    @Autowired
    private GlobalService globalService;

    @GetMapping("/search")
    @ApiOperation(value = "搜索")
    public ApiResult<SearchVo> search(@RequestParam("name") String name, @RequestParam(value = "type", required = false) Integer type, HttpServletRequest request) {
        return searchService.search(name, type);
    }

    @GetMapping("/searchOrder")
    @ApiOperation(value = "搜索订单")
    public ApiResult<List<OrderVo>> searchOrder(@RequestParam(value = "direction", required = false) Integer direction, @RequestParam("name") String name, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return searchService.searchOrder(userId, direction, name);
    }

    @PostMapping("/searchCollection")
    @ApiOperation(value = "搜索统计")
    public ApiResult<Void> searchCollection(@RequestBody SearchCollectionReqDto searchCollectionReqDto, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return searchService.searchCollection(searchCollectionReqDto, userId);
    }

    @GetMapping("/searchTopList")
    @ApiOperation(value = "热搜列表")
    public ApiResult<List<SearchVo.SearchItem>> getTopList(@RequestParam(value = "type", required = false) Integer type, HttpServletRequest request) {
        return searchService.getTopList(type, 1, 5);
    }

    @GetMapping("/getNoticeContent")
    @ApiOperation(value = "通告栏内容")
    public ApiResult<String> getNoticeContent() {
        return globalService.getNoticeContent();
    }

    @GetMapping("/getReportDataList")
    @ApiOperation(value = "首页统计数据列表")
    public ApiResult<List<List<ReportDataVo>>> getReportDataList(HttpServletRequest request) {
        Long userId = StringUtils.isNotBlank(request.getHeader(Constant.AUTHORIZATION)) ? JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION)) : null;
        return globalService.getReportDataList(userId);
    }

    @GetMapping("/getGuessList")
    @ApiOperation(value = "获取首页推荐图书列表")
    public ApiResult<PageResponse<List<SearchVo.SearchItem>>> getGuessList(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        return globalService.getGuessList(page, size);
    }

    @PostMapping("/feedback")
    @ApiOperation(value = "提交意见反馈")
    public ApiResult<BkFeedback> feedback(@RequestBody FeedbackReqDto feedbackReqDto, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return globalService.feedback(feedbackReqDto, userId);
    }

    @GetMapping("/about")
    @ApiOperation(value = "关于")
    public ApiResult<AboutVo> about() {
        return globalService.about();
    }

    @PostMapping("/birthdayRemind")
    @ApiOperation(value = "生日提醒")
    public ApiResult<Boolean> birthdayRemind(@RequestBody BirthdayRemindReqDto birthdayRemindReqDto, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        log.info("reqDto:{}", birthdayRemindReqDto);
        return globalService.birthdayRemind(birthdayRemindReqDto, userId);
    }

    @PostMapping("/generateTalkRecord")
    @ApiOperation(value = "生成微信聊天图片")
    public ApiResult<BkTalkRecord> generateTalkRecord(@Valid @RequestBody GenerateTalkRecordReqDto generateTalkRecordReqDto, HttpServletRequest request, HttpServletResponse response) {
        Long myUserId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return globalService.generateTalkRecord(generateTalkRecordReqDto, myUserId);
    }

}
